﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using VipCo.Data;
using VipCo.Entities;

public partial class Bai_viet : System.Web.UI.Page
{
    public int IdSub;

    protected void Page_Load(object sender, EventArgs e)
    {
        string idSub = Request.Params["Id"];
        if (idSub == null)
        {
            HienthiTinBVH(Pager1.CurrentIndex, 0);
            IdSub = 0;
        }
        else
        {
            IdSub = Convert.ToInt32(idSub);
            HienthiTinBVH(Pager1.CurrentIndex, IdSub);
        }
    }

    /// <summary>
    /// Hien thi tin tuc khuyen mai
    /// </summary>
    /// <param name="pageNo"></param>
    /// <param name="idSubtintuc"></param>
    protected void HienthiTinBVH(int pageNo, int idSubtintuc)
    {
        // Lay ID tin Khuyen mai
        int totalRows = 0;
        string whereClause = "TenNhom like N'" + Common.NhomTinTuc_Baiviethay + "' and TrangThai = 1";        

        TList<NhomTinTuc> tlistNhomTinTucs = DataRepository.NhomTinTucProvider.GetPaged(whereClause, "Id DESC", 0, 0,
            out totalRows);

        // 
        if (tlistNhomTinTucs.Count > 0)
        {
            // Hien thi the loai tin tuc
            HienthiTheLoai(tlistNhomTinTucs[0].Id);

            // Thuc hien lay danh sach cac tin Khuyen Mai
            string dieuKien = "IdNhomTin = " + tlistNhomTinTucs[0].Id + " and TrangThai = 1";
            if (idSubtintuc != 0)
            {
                dieuKien = "IdNhomTin = " + tlistNhomTinTucs[0].Id + " and TrangThai = 1 and IdSubTin = " + idSubtintuc;

                // Hien thi ten sub tin
                SubTinTuc entSubtintuc = DataRepository.SubTinTucProvider.GetById(idSubtintuc);
                lbSubtin.Visible = true;
                lbSubtin.Text = entSubtintuc.TenSubTinTuc;
            }
            TList<TinTuc> tlistTinTucs = DataRepository.TinTucProvider.GetPaged(dieuKien, "NgayCapNhat DESC", pageNo - 1, Pager1.PageSize, out totalRows);

            // Bind du lieu vao datalist
            dlTinKhuyenMai.DataSource = tlistTinTucs;
            dlTinKhuyenMai.DataBind();

            // Hien thi so luong trang
            TList<TinTuc> tlistTinTucs1 = DataRepository.TinTucProvider.GetPaged(dieuKien, "NgayCapNhat DESC", 0, 0, out totalRows);

            if (tlistTinTucs1.Count > 0)
            {
                Pager1.ItemCount = tlistTinTucs1.Count;
                if (tlistTinTucs1.Count < Pager1.PageSize)
                {
                    Pager1.Visible = false;
                }
                else
                {
                    Pager1.Visible = true;
                }
            }
        }
    }

    public string GetTieuDe(object Char)
    {
        string s = Char.ToString();
        return Common.ConvertToUnSign(s);
    }

    public string formatString(string str, int n)
    {
        string kq = "";

        if (str.Length > n)
            kq = str.Substring(0, n + 1) + "...";
        else
            kq = str;

        return kq;
    }

    protected void HienthiTheLoai(int idNhomtintuc)
    {
        int totalRows = 0;
        string whereClause = "IdNhomTinTuc = " + idNhomtintuc + " and TrangThai = 1";

        // Lay danh sach SubTinTuc cua loai tin KhuyenMai
        TList<SubTinTuc> tlistSubtintuc = DataRepository.SubTinTucProvider.GetPaged(whereClause, "Id DESC", 0, 0,
                                                                                    out totalRows);
        if (tlistSubtintuc.Count > 0)
        {
            dlSubtintuc.DataSource = tlistSubtintuc;
            dlSubtintuc.DataBind();
        }
    }

    protected void Pager1_Command(object sender, CommandEventArgs e)
    {
        int currnetPageIndex = Convert.ToInt32(e.CommandArgument);
        Pager1.CurrentIndex = currnetPageIndex;
        HienthiTinBVH(currnetPageIndex, IdSub);
    }
}